package se.rkrantz.simpleraid.hibernate.dao.impl;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import se.rkrantz.simpleraid.hibernate.dao.UserDao;
import se.rkrantz.simpleraid.hibernate.domain.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Override
	public void save(User user) {
		getHibernateTemplate().save(user);
	}

	@Override
	public void update(User user) {
		getHibernateTemplate().update(user);
	}

	@Override
	public void delete(User user) {
		getHibernateTemplate().delete(user);
	}

	@Override
	public User findUserByUsername(String username) {
		return (User) getHibernateTemplate().get(User.class, username);
	}

	public void setPrimaryCharacter(User user, String characterId) {
		Query query = getSession().createQuery("update User set primaryCharacter_CHARACTER_ID = :charId where USERNAME = :username");
		query.setString("charId", characterId);
		query.setString("username", user.getUsername());
		query.executeUpdate();
	}

}
